/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement.generator;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.yonsei.iwb.sdt.SDTLearner;
import edu.yonsei.iwb.sdt.SDTLearningType;
import java.util.List;
import java.util.Vector;
import edu.yonsei.iwb.sdt.algorithm.DCSSearcher;
import edu.yonsei.iwb.sdt.datastructure.tree.BranchingType;
import edu.yonsei.iwb.sdt.datastructure.DecomposeCandidate;
import edu.yonsei.iwb.sdt.datastructure.DecomposeCandidateSet;
import edu.yonsei.iwb.sdt.datastructure.TargetInstance;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;
import edu.yonsei.iwb.sdt.refinement.DLConstructors;
import edu.yonsei.iwb.sdt.refinement.ExpandingHistory;
import edu.yonsei.iwb.sdt.refinement.ExpandingHistorySet;
import edu.yonsei.iwb.sdt.refinement.QualificationRefinement;
import edu.yonsei.iwb.sdt.refinement.Refinement;
import edu.yonsei.iwb.sdt.refinement.RefinementType;
import edu.yonsei.iwb.sdt.util.SDTLog;
import edu.yonsei.iwb.ts.TSSearch;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author JDK
 */
public class QualificationRefinementMaker extends Thread {

    private Vector<TargetInstance> instanceSet;
    private PropertyArc pa;
    private String conclass;
    private Vector<PropertyArc> hisPAset;
    private DecomposeCandidateSet dcs;
    private QuantificationRefinementMaker qrm;

    public QualificationRefinementMaker(Vector<TargetInstance> instanceSet) {
        this.instanceSet = instanceSet;
        this.qrm = new QuantificationRefinementMaker(RefinementType.QUALIFICATION);
    }

    public QualificationRefinementMaker(PropertyArc pa, String conclass, Vector<PropertyArc> hisPAset, Vector<TargetInstance> instanceSet, DecomposeCandidateSet dcs) {
        this.hisPAset = hisPAset;
        this.pa = pa;
        this.conclass = conclass;
        this.dcs = dcs;
        this.instanceSet = instanceSet;
        this.qrm = new QuantificationRefinementMaker(RefinementType.QUALIFICATION);
    }

    @Override
    public synchronized void run() {
        try {
            long startTime = System.currentTimeMillis();
            SDTLog.log(SDTLog.DETAIL, "# Generating start - QR : Standard = " + this.conclass + " / Property : " + pa.toString() + "\n");
            boolean gen = decomposeCandidateOfQR();
            DCSSearcher.FLAGNUM--;
            long endTime = System.currentTimeMillis();
            if (gen) {
                SDTLog.log(SDTLog.DETAIL, "# QR : Property : " + pa.toString() + " / " + this.conclass + " history PA set : "+this.hisPAset+" is generated..." + ((endTime - startTime) / 1000.0f) + "s)\n");
            } else {
                SDTLog.log(SDTLog.DETAIL, "# - QR : Property : " + pa.toString() + " / " + this.conclass + " history PA set : "+this.hisPAset+" is NOT generated..." + ((endTime - startTime) / 1000.0f) + "s)\n");
            }
        } catch (IOException ex) {
            Logger.getLogger(QualificationRefinementMaker.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private boolean decomposeCandidateOfQR() throws IOException {
        DecomposeCandidateSet tempdcs = refinementGenerator(pa, conclass, hisPAset);
        if (!tempdcs.isEmpty()) {
            dcs.addAll(tempdcs);
            return true;
        } else {
            return false;
        }
    }

    public DecomposeCandidateSet refinementGenerator(PropertyArc pa, String subclass, Vector<PropertyArc> propertySet) throws IOException {
        ArrayList<String> res = TSSearch.listSubjectResource(RDF.type.getURI(), subclass);
        Vector<String> css = new Vector(res);
        Vector<PropertyArc> propertyHistory = new Vector(propertySet);
        propertyHistory.add(pa);

        ExpandingHistorySet ehs = this.qrm.expandingHistoryCounter(propertyHistory, css);
        DecomposeCandidateSet dcs = new DecomposeCandidateSet();

        for (int i = 0; i < ehs.size(); i++) {
            ExpandingHistory eh = ehs.get(i);
            Vector<Refinement> refset = new Vector<Refinement>();

            Vector<String> is = eh.getInstanceSet();

//            UtilFunctions.printVectorOfResource(is);

            Vector<TargetInstance> cloneSet = new Vector(this.instanceSet);
            Vector<TargetInstance> sfTIset = new Vector();

            for (int j = 0; j < is.size(); j++) {
                String result = is.get(j);
                for (int k = 0; k < this.instanceSet.size(); k++) {
                    TargetInstance ti = this.instanceSet.get(k);
                    if (ti.getURI().equals(result)) {
                        sfTIset.add(ti);
                        cloneSet.remove(ti);
                    }
                }
            }

            if (!sfTIset.isEmpty()) {
                Refinement pref = new QualificationRefinement("", eh, subclass);
                pref.setInstanceSet(sfTIset);
                refset.add(pref);
            }

            if (!cloneSet.isEmpty()) {
                Refinement nref = new QualificationRefinement(DLConstructors.NEGATION, eh, subclass);
                nref.setBranchingType(BranchingType.NEGATION_TYPE);
                nref.setInstanceSet(cloneSet);
                refset.add(nref);
            }
            
            if (refset.size() > 1) {
                DecomposeCandidate dc = new DecomposeCandidate(refset, pa, RefinementType.QUALIFICATION);                
                dcs.add(dc);
            }            
        }

        return dcs;
    }
}
